package org.funtester.performance.books.chapter03.common;

import java.util.Collections;
import java.util.List;

/**
 * 数据处理工具类
 */
public class DataHandleTool {

    /**
     * 统计数据
     * @param data
     */
    public static Quantile statisticData(List<Integer> data) {
        Collections.sort(data);// 排序
        int min = data.get(0);// 最小值
        int max = data.get(data.size() - 1);// 最大值
        int average = (int) data.stream().mapToInt(x -> x).average().getAsDouble();// 平均值
        int p50 = data.get((int) (0.5 * data.size()));// 50分位值
        int p90 = data.get((int) (0.9 * data.size()));// 90分位值
        int p95 = data.get((int) (0.95 * data.size()));// 95分位值
        int p99 = data.get((int) (0.99 * data.size()));// 99分位值
        int p999 = data.get((int) (0.999 * data.size()));// 999分位值
        return new Quantile(min, max, average, p50, p90, p95, p99, p999);
    }

    /**
     * 统计数据,各个分位值
     */
    public static class Quantile {

        public int min;

        public int max;

        public int average;

        public int p50;

        public int p90;

        public int p95;

        public int p99;

        public int p999;

        public Quantile(int min, int max, int average, int p50, int p90, int p95, int p99, int p999) {
            this.min = min;
            this.max = max;
            this.average = average;
            this.p50 = p50;
            this.p90 = p90;
            this.p95 = p95;
            this.p99 = p99;
            this.p999 = p999;
        }

        public void print() {
            // 打印统计结果
            System.out.println("最小值：" + this.min);
            System.out.println("最大值：" + this.max);
            System.out.println("平均值：" + this.average);
            System.out.println("50分位值：" + this.p50);
            System.out.println("90分位值：" + this.p90);
            System.out.println("95分位值：" + this.p95);
            System.out.println("99分位值：" + this.p99);
            System.out.println("999分位值：" + this.p999);
        }

    }

}
